<template>
	<navbar title="支付" :isBack="true"></navbar>
	<view class=""
		style="margin-top: 100rpx;display: flex;flex-direction: column;justify-content: center;align-items: center;padding: 0 30rpx;">
		<view class="" style="font-size: 32rpx;">
			支付金额
		</view>
		<view class="" style="margin-top: 30rpx;font-size: 80rpx;font-weight: bold;">
			<span style="font-size: 32rpx;">￥</span>{{param.pay_price}}<span style="font-size: 32rpx;"></span>
		</view>
	</view>
	<view class=""
		style="margin-top: 100rpx;display: flex;justify-content: space-between;align-items: center;padding: 0 30rpx;">
		<view class="" style="display: flex;align-items: center;">
			<image src="/static/image/we.png" style="width: 40rpx;height: 40rpx;" mode=""></image>
			<view class="" style="margin-left: 10rpx;font-size: 32rpx;">
				微信支付
			</view>
		</view>
		<image src="/static/image/radio-active.png" style="width: 40rpx;height: 40rpx;" mode=""></image>
	</view>
	<view class="" style="padding: 0 30rpx;">
		<view class="" @click="pay"
			style="height: 100rpx;background: linear-gradient(90deg, #5062A7 0%, #354678 100%);border-radius: 10rpx;display: flex;justify-content: center;align-items: center;color: white;font-weight: bold;font-size: 32rpx;margin-top: 100rpx;">
			确认支付
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import { ref, reactive } from 'vue';
	import tools from '@/utils/tools'
	import { cashier, orderPay } from '@/service/api/index'
	import { speedOrderPay, speedCashier } from '@/service/api/speed'
	import { cityCashier, cityOrderPay } from '@/service/api/city'
	import { shopCashier,shopOrderPay } from '@/service/api/shop'
	const id = ref('')
	const param = reactive({
		id: "",
		pay_price: "",
		state: "",
		time: ""
	})
	const type = ref(0)//1是七天速成支付 3是商城订单支付
	onLoad((options) => {
		id.value = options.id;
		if (options.type) {
			type.value = options.type;
		}
		getData()
	})

	const getData = async () => {
		if (type.value == 1) {
			//七天速成收银台
			const res = await speedCashier({ id: id.value });
			for (let i in param) {
				param[i] = res.msg[i];
			}
			return
		}
		if (type.value == 2) {
			//城市巡回
			const res = await cityCashier({ id: id.value });
			for (let i in param) {
				param[i] = res.msg[i];
			}
			return
		}
		if (type.value == 3) {
			//商城订单支付
			const res = await shopCashier({id:id.value})
			for (let i in param) {
				param[i] = res.msg[i];
			}
			return
		}
		const res = await cashier({ id: id.value })
		for (let i in param) {
			param[i] = res.msg[i];
		}

	}
	const pay = async () => {
		//七天速成支付
		if (type.value == 1) {
			speedPay()
			return
		}
		//城市巡回支付
		if (type.value == 2) {
			cityPay()
			return
		}
		if(type.value == 3){
			//商城订单支付
			goodsPay()
			return;
		}
		//课程支付
		const res = await orderPay({ id: param.id });

		if (res.code == 1) {
			tools.requestPayment(res.msg).then((result) => {
				console.log('正常' + result);
				uni.redirectTo({
					url: '/pagesB/order/paySuc?type' + type.value
				})
			}).catch((err) => {
				console.log('失败' + err);
				tools.navTo('/pagesB/order/payFail?id=' + param.id + '&type=' + type.value, 4)
				// uni.redirectTo({
				// 	url: '/pagesB/order/payFail'
				// })
			});
		}
		return

	}
	const speedPay = async () => {
		const res = await speedOrderPay({ id: param.id })
		if (res.code == 1) {
			tools.requestPayment(res.msg).then((result) => {
				console.log('正常' + result);
				uni.redirectTo({
					url: '/pagesB/order/paySuc?type=' + type.value
				})
			}).catch((err) => {
				console.log('失败' + err);
				tools.navTo('/pagesB/order/payFail?id=' + param.id + '&type=' + type.value, 4)

			});
		}
	}
	const cityPay = async () => {
		const res = await cityOrderPay({ id: param.id })
		if (res.code == 1) {
			tools.requestPayment(res.msg).then((result) => {
				console.log('正常' + result);
				uni.redirectTo({
					url: '/pagesB/order/paySuc?type=' + type.value
				})
			}).catch((err) => {
				console.log('失败' + err);
				tools.navTo('/pagesB/order/payFail?id=' + param.id + '&type=' + type.value, 4)

			});
		}
	}
	const goodsPay = async()=>{
		const res = await shopOrderPay({ id: param.id })
		if (res.code == 1) {
			tools.requestPayment(res.msg).then((result) => {
				console.log('正常' + result);
				uni.redirectTo({
					url: '/pagesB/order/paySuc?type=' + type.value
				})
			}).catch((err) => {
				console.log('失败' + err);
				tools.navTo('/pagesB/order/payFail?id=' + param.id + '&type=' + type.value, 4)
		
			});
		}
	}
</script>

<style scoped lang="scss">
</style>